package com.ssy.system.controller;

import com.ssy.common.Constant;
import com.ssy.common.utils.PageResult;
import com.ssy.common.utils.Result;
import com.ssy.system.pojo.Notice;
import com.ssy.system.pojo.User;
import com.ssy.system.service.INoticeService;
import com.ssy.system.vo.NoticeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * @author ssy
 */
@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private INoticeService noticeService;

    /**
     * 公告的分页查询
     *
     * @param noticeVo
     * @return
     */
    @GetMapping("/loadAllNoticePage")
    public PageResult loadAllNoticePage(NoticeVo noticeVo) {
        return this.noticeService.loadAllNoticePage(noticeVo);
    }

    /**
     * 添加公告
     *
     * @param notice
     * @return
     */
    @PostMapping("/addNotice")
    public Result addNotice(@RequestBody Notice notice, HttpSession session) {
        User user = (User) session.getAttribute(Constant.SESSION_CURRENT_USER);
        notice.setCreateName(user.getUserName());
        notice.setCreateTime(new Date());
        return this.noticeService.insertNotice(notice);
    }

    /**
     * 修改公告
     *
     * @param notice
     * @return
     */
    @PostMapping("/updateNotice")
    public Result updateNotice(@RequestBody Notice notice) {
        return this.noticeService.updateNotice(notice);
    }

    /**
     * 删除公告
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteNotice/{id}")
    public Result deleteNotice(@PathVariable Integer id) {
        return this.noticeService.deleteNoticeById(id);
    }

    /**
     * 批量删除公告
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/bathDeleteNotice/{ids}")
    public Result bathDeleteNotice(@PathVariable Integer[] ids) {
        return this.noticeService.bathDeleteNotice(ids);
    }


}
